package com.yonyou.brigade.third_parties.third.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yonyou.brigade.third_parties.third.model.BriGroupMember;
import com.yonyou.brigade.third_parties.third.dao.BriGroupMemberMapper;
import com.yonyou.brigade.third_parties.third.model.BriGroupMemberExt;
import com.yonyou.brigade.third_parties.third.service.IBriGroupMemberService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangzhf@yonyou.com
 * @since 2018-08-16
 */
@Service
public class BriGroupMemberServiceImpl extends ServiceImpl<BriGroupMemberMapper, BriGroupMember> implements IBriGroupMemberService {


    @Autowired
    private BriGroupMemberMapper groupMemberMapper;

    public Map findGroupMembersByFilter(int groupid, int pageSize, int current, String conditin)
    {
        if(current<=0)
        {
            current=1;
        }
        Map result=new HashMap();
        int start=(current-1)*pageSize;
        int size=pageSize;
        try {
            Map params=new HashMap();
            params.put("start",start);
            params.put("size",size);
            params.put("groupid",groupid);
            params.put("condition",conditin);
            int total=groupMemberMapper.getGroupMembersByFilterCount(params);
            List<BriGroupMemberExt> groupMembers=groupMemberMapper.getGroupMembersByFilter(params);
            result.put("code","0");
            result.put("information",groupMembers);
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }
        catch (Exception e)
        {
            result.put("code","1");
            result.put("information","操作失败");
        }
        return  result;
    }

    public  List<BriGroupMemberExt> getGroupMembersByUserids(Map params)
    {
        return groupMemberMapper.getGroupMembersByUserids(params);
    }

    public  Map updateNote(String userid,String note)
    {
        Map result=new HashMap();
        try {
            Map params=new HashMap();
            params.put("userid",userid);
            params.put("note",note);
            groupMemberMapper.updateNote(params);
            result.put("code","0");
            result.put("information","操作成功");
        }
        catch (Exception e)
        {
            result.put("code","1");
            result.put("information","操作失败");
        }
        return result;
    }


    public Map updateGid(String gid,List<String> userids)
    {
        Map result=new HashMap();
        try {
            Map params=new HashMap();
            params.put("gid",gid);
            params.put("list",userids);

            groupMemberMapper.updateGid(params);
            result.put("code","0");
            result.put("information","操作成功");
        }
        catch (Exception e)
        {
            result.put("code","1");
            result.put("information","操作失败");
        }
        return result;
    }

    public Map deleteguid(String gid,List<String> userids)
    {
        Map result=new HashMap();
        try {
            Map params=new HashMap();
            params.put("gid",gid);
            params.put("list",userids);

            groupMemberMapper.deleteGroupUserByUserId(params);
            result.put("code","0");
            result.put("information","操作成功");
        }
        catch (Exception e)
        {
            result.put("code","1");
            result.put("information","操作失败");
        }
        return result;
    }

    public List<BriGroupMember> findGroupMembersByTenantOrgChannel(String tenant,String orgPk,String channel)
    {
        Wrapper<BriGroupMember> briGroupMemberWrapper=new EntityWrapper();
        briGroupMemberWrapper.where("tenant={0}",tenant);
        briGroupMemberWrapper.and("org_pk={0}",orgPk);
        List<BriGroupMember> briGroupMembers=groupMemberMapper.selectList(briGroupMemberWrapper);
        return  briGroupMembers;
    }

    public List<String> findGroupMemberIdsByTenantOrgChannel(String tenant,String orgPk,String channel)
    {
        List<String> ids=new ArrayList<>();
        List<BriGroupMember> briGroupMembers=findGroupMembersByTenantOrgChannel(tenant,orgPk,channel);
        if(briGroupMembers!=null&&briGroupMembers.size()>0)
        {
            for(BriGroupMember b:briGroupMembers)
            {
                ids.add(b.getUserId());
            }
        }
        return ids;
    }

}
